package com.woniuxy.shirocore;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @ClassName LoginAuthBasicDemo
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/5 15:02
 * @Version 1.0
 */
public class LoginAuthBasicDemo {
    public static void main(String[] args) {
        //1、读取配置文件
        BasicIniEnvironment basicIniEnvironment= new BasicIniEnvironment("classpath:ini/shiro-realm.ini");
        //2、创建SecurityManager中央大脑
        SecurityManager securityManager= basicIniEnvironment.getSecurityManager();
        //3、使用工具类，注入中央大脑
        SecurityUtils.setSecurityManager(securityManager);
        //4、通过工具类，创建Subject对象
        Subject subject= SecurityUtils.getSubject();
//        5、模拟前端传入后端的账号，交给中央大脑来帮我们判断是否可以登录
        subject.login(new UsernamePasswordToken("admin","123456"));
        //6.如果登录成功,isAuthenticated() 过去式，true表示登录过了。false
        System.out.println(subject.getPrincipal()+"登录成功："+subject.isAuthenticated());
        subject.checkRole("admin");
        subject.checkPermission("manager:addOne");
    }
}
